/*
 * Copyright [2013-2021], Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.polardbx.optimizer.sharding.label;

import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.util.mapping.Mapping;

import java.util.EnumSet;
import java.util.List;

/**
 * @author chenmo.cm
 */
public interface LabelOptNode {

    /**
     * @return the rowType generated by the root node of the operator subtree which
     * belongs to this label node.
     */
    RelDataType getRowType();

    /**
     * getRowType() without cache
     */
    RelDataType deriveRowType();

    /**
     * Equals take {@link LabelOptNode#getRowType()} into Consideration
     *
     * @param that another {@link LabelOptNode}
     * @return this.equals(that) && this.rowType == that.rowType
     */
    boolean equalsOpt(LabelOptNode that);

    /**
     * @return typeSet.contains(this.getType () )
     */
    boolean is(EnumSet<LabelType> typeSet);

    /**
     * @return the bottom node of the operator subtree which belongs to this label
     * node
     */
    <R extends RelNode> R getRel();

    /**
     * @return the {@link LabelType} of this label node
     */
    LabelType getType();

    /**
     * @return full column information for Semi/Anti Join, more detail in
     * {@link FullRowType}
     */
    FullRowType getFullRowType();

    /**
     * @return column Mapping from current projected {@link #getRowType()} to origin
     * rowType of {@link #getRel()}
     */
    Mapping getColumnMapping();

    /**
     * @return an array marking which column of {@link #getRowType()} is originally an expression
     */
    PredicateNode[] getColumnConditionMap();

    /**
     * Whether all elements in {@link #getColumnConditionMap()} are null
     */
    boolean withoutExpressionColumn();

    /**
     * Whether any elements in {@link #getColumnConditionMap()} is not null
     */
    boolean withExpressionColumn();

    PredicateNode getPullUp();

    void setPullUp(PredicateNode pullUp);

    PredicateNode getPushdown();

    void setPushdown(PredicateNode pushdown);

    PredicateNode getCorrelatePushdown();

    void setCorrelatePushdown(PredicateNode correlatePushdown);

    List<PredicateNode> getPredicates();
}
